package stardust.core.network.protocol.http.html;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import stardust.core.io.StreamArray;


public class Tag
{

  private String name;

  private String value;

  private Map<String, String> attribute;

  private List<Tag> child;

  private Tag parent;

  public Tag()
  {
  }

  public Tag(Tag parent, String name)
  {
    this(parent, name, null);
  }

  public Tag(Tag parent, String name, String value)
  {
    this.parent = parent;
    this.name = name;
    this.value = value;
  }

  public String getName()
  {
    return name;
  }

  public void setName(String name)
  {
    this.name = name;
  }

  public String getValue()
  {
    return value;
  }

  public void setValue(String value)
  {
    this.value = value;
  }

  public boolean hasChild()
  {
    return this.child != null && !this.child.isEmpty();
  }

  public List<Tag> getChild()
  {
    if (this.child == null)
    {
      this.child = new ArrayList<Tag>();
    }

    return child;
  }

  public Map<String, String> getAttribute()
  {
    if (this.attribute == null)
    {
      this.attribute = new HashMap<String, String>();
    }

    return attribute;
  }

  public String toString()
  {
    return "<" + this.name + ">" + this.getChild() + "</" + this.name + ">";
  }

  public static void parseHTML(StreamArray reader, Tag parent) throws IOException
  {
    Tag tag;
    Character c;

nc: while (true)
    {
      c = reader.readChar();

      if (c == null)
      {
        break;
      }

      /*
       * 标记开始。<html>
       */
      if (c == '<')
      {
        String tagName = "";
        boolean isExistsAttribute = false;

        while ((c = reader.readChar()) != null)
        {
          // 标记结束。<html>
          if (c == '>')
          {
            break;
          }
          else if (c == '/' && tagName.trim().isEmpty())
          {
            // 这是一个结束标记。</html>
            String endTag = reader.readTo(">");

            System.out.println("读取到结束标记" + endTag);
            System.out.println("寻找对应的开始标记...");
            // while ()
            // {
            //
            // }
          }
          else if (c == '/' && !tagName.trim().isEmpty())
          {
            // 这是一个短结束标记。<html />
            String endTag = reader.readTo(">");

            if (endTag.trim().isEmpty())
            {
              break;
            }
            else
            {
              tagName += c + endTag;
              break;
            }
          }

          tagName += c;
        }

        tag = new Tag(parent, tagName);
        parent.getChild().add(tag);
      }
    }
  }

  public static void main(String[] args) throws IOException
  {
    Tag r = new Tag();
    StreamArray reader = new StreamArray("<html>ABCD</html>".getBytes());
    parseHTML(reader, r);

    System.out.println(r.getChild());
  }

}
